articles

Home / DeveloperSection / Articles / Accessing Properties of Photo Library using ALAssetsLibrary in iOS-6

Accessing Properties of Photo Library using ALAssetsLibrary in iOS-6

Tarun Kumar4142 21-Dec-2015

In this article we will describe how we can access images and videos programmatically under the Photos application. If we want to access images/videos then it is necessary to having knowledge how to get properties of Photo application that contains saved photos, imported from iTunes and imported directly into the device. So below we will see an example to get properties of photo library using ALAssetsLibrary.

Now, first of all we have to consider on 3 elements:

1.     ALAssetsLibrary – It is used as a list of “groups”, the main class is ALAssetsLibrary. It provides us a facility in order to navigate into Photos Library.

Accessing Properties of Photo Library using ALAssetsLibrary in iOS-6 


2.   ALAssetsGroup – It is used as list of “assets”, ALAssetsLibrary allows us to find groups and ALAssetsGroup will be the actual resource such as “Saved Photos”.


Accessing Properties of Photo Library using ALAssetsLibrary in iOS-6 


3.       ALAsset – It is used as list of multimedia elements, ALAssetsGroup allows us to find the list of final resources (ALAsset).

Accessing Properties of Photo Library using ALAssetsLibrary in iOS-6

 

Now, the conclusion is that in order to get all resources we have to enumerate ALAssetsGroup from ALAsstetsLibrary and for each group we have to enumerate ALAssets.

For using ALAssetsLibrary follow the steps:

1.       First of all add the AssetsLibrary.framework from the “Linked Frameworks and Libraries”, like this:


Accessing Properties of Photo Library using ALAssetsLibrary in iOS-6 


Now, in our code import it like this:

#import< AssetsLibrary/AssetsLibrary.h> 

And after open the starting point of AppDelegate method didFinishLaunchingWithOptions: and use the following code:   

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: 
(NSDictionary *)launchOptions {

   self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
   // Override point for customization after application launch.
   self.window.backgroundColor = [UIColor whiteColor];
   [self.window makeKeyAndVisible];
   UIImageView *imgView = [[UIImageView alloc] initWithFrame:self.window.frame];
   [self.window addSubview:imgView];
   ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
   [library enumerateGroupsWithTypes:ALAssetsGroupAll

   usingBlock: ^(ALAssetsGroup *group, BOOL *stop) {
     // ALAssetsGroup Properties
     NSLog(@"Group Name : '%@'", [group valueForProperty:ALAssetsGroupPropertyName]);
     NSLog(@"Group Type : '%@'", [group valueForProperty:ALAssetsGroupPropertyType]);
     NSLog(@"Group ID : '%@'", [group valueForProperty:ALAssetsGroupPropertyPersistentID]);
     NSLog(@"Group URL : '%@'", [group valueForProperty:ALAssetsGroupPropertyURL]);

     [group enumerateAssetsUsingBlock: ^(ALAsset *result, NSUInteger index, BOOL *stop) {
       // ALAssets Properties
       NSLog(@"Stop? : %@", (stop ? @"YES" : @"NO") );
       NSLog(@"Assets Type : '%@'", [result valueForProperty:ALAssetPropertyType]);
       NSLog(@"Assets Location : '%@'", [result valueForProperty:ALAssetPropertyLocation]);
       NSLog(@"Assets Duration : '%@'", [result valueForProperty:ALAssetPropertyDuration]);
       NSLog(@"Assets Orientation : '%@'", [result valueForProperty:ALAssetPropertyOrientation]);
       NSLog(@"Assets created Date : '%@'", [result valueForProperty:ALAssetPropertyDate]);
       NSLog(@"Assets Representations : '%@'", [result valueForProperty:ALAssetPropertyRepresentations] objectAtIndex:0]);
       NSLog(@"Assets URLs : '%@'", [result valueForProperty:ALAssetPropertyURLs] objectForKey:[[result valueForProperty:ALAssetPropertyRepresentations] objectAtIndex:0]]);
       if ([@"Sample.jpeg" caseInsensitiveCompare:[[result valueForProperty:ALAssetPropertyRepresentations] objectAtIndex:0]] == NSOrderedSame ) {
          NSLog(@"Image : '%@'", [result valueForProperty:ALAssetPropertyURLs] objectForKey:@"public.jpeg"]);
          *stop = YES; // if you want to remove in order to list all resources use this
          ALAssetRepresentation *assetRepresentation = [result defaultRepresentation];
          UIImage *img = [UIImage imageWithCGImage: [assetRepresentation fullResolutionImage]];
          [imgView setImage:img];
       }
     }];
   }

   // if the above block will not able to execute then the following block will execute
   failureBlock: ^(NSError *error) {
   NSLog(@"Failure");
  }];
  return YES;
}

In the above code we use a class named ALAssetRepresentation. Its object is used to encapsulates one of the representations of a given ALAsset object.

When we click on Run button on X-Code, the output will display in the console and an image will displ on the storyboard, the output look like this:  


Output:

2015-12-21 18:33:03.588 ALAssetsLibrary Example[867:18e03] Application windows are expected to have a root view controller at the end of application launch 2015-12-21 18:33:03.624 ALAssetsLibrary Example[867:18e03] ........................ 2015-12-21 18:33:03.625 ALAssetsLibrary Example[867:18e03] Group Name : 'Try' 2015-12-21 18:33:03.626 ALAssetsLibrary Example[867:18e03] Group Type : '2' 2015-12-21 18:33:03.627 ALAssetsLibrary Example[867:18e03] Group ID : '9BC47750-2264-458E-BC51-8F5E78D61AEC' 2015-12-21 18:33:03.628 ALAssetsLibrary Example[867:18e03] Group URL : 'assets-library://group/?id=9BC47750-2264-458E-BC51-8F5E78D61AEC' 2015-12-21 18:33:03.630 ALAssetsLibrary Example[867:18e03] Stop? : YES 2015-12-21 18:33:03.631 ALAssetsLibrary Example[867:18e03] Assets Type : '(null)' 2015-12-21 18:33:03.633 ALAssetsLibrary Example[867:18e03] Assets Location : '(null)' 2015-12-21 18:33:03.635 ALAssetsLibrary Example[867:18e03] Assets Duration : '(null)' 2015-12-21 18:33:03.636 ALAssetsLibrary Example[867:18e03] Assets Orientation : '(null)' 2015-12-21 18:33:03.637 ALAssetsLibrary Example[867:18e03] Assets created Date : '(null)' 2015-12-21 18:33:03.639 ALAssetsLibrary Example[867:18e03] Assets Representations : '(null)' 2015-12-21 18:33:03.640 ALAssetsLibrary Example[867:18e03] Assets URLs : '(null)' 2015-12-21 18:33:03.642 ALAssetsLibrary Example[867:18e03] ........................ 2015-12-21 18:33:03.644 ALAssetsLibrary Example[867:18e03] Group Name : 'Saved Photos' 2015-12-21 18:33:03.645 ALAssetsLibrary Example[867:18e03] Group Type : '16' 2015-12-21 18:33:03.645 ALAssetsLibrary Example[867:18e03] Group ID : '1F157B65-16D2-452B-A33B-6C2872BC5349' 2015-12-21 18:33:03.646 ALAssetsLibrary Example[867:18e03] Group URL : 'assets-library://group/?id=1F157B65-16D2-452B-A33B-6C2872BC5349' 2015-12-21 18:33:03.646 ALAssetsLibrary Example[867:18e03] Stop? : YES 2015-12-21 18:33:03.648 ALAssetsLibrary Example[867:18e03] Assets Type : 'ALAssetTypePhoto' 2015-12-21 18:33:03.648 ALAssetsLibrary Example[867:18e03] Assets Location : '(null)' 2015-12-21 18:33:03.648 ALAssetsLibrary Example[867:18e03] Assets Duration : 'ALErrorInvalidProperty' 2015-12-21 18:33:03.650 ALAssetsLibrary Example[867:18e03] Assets Orientation : '0' 2015-12-21 18:33:03.651 ALAssetsLibrary Example[867:18e03] Assets created Date : '2007-08-14 17:04:42 +0000' 2015-12-21 18:33:03.653 ALAssetsLibrary Example[867:18e03] Assets Representations : 'public.jpeg' 2015-12-21 18:33:03.656 ALAssetsLibrary Example[867:18e03] Assets URLs : 'assets-library://asset/asset.JPG?id=D283C9AB-8B81-46BE-B9F1-F107C2327B68&ext=JPG' 2015-12-21 18:33:03.657 ALAssetsLibrary Example[867:18e03] Stop? : YES 2015-12-21 18:33:03.658 ALAssetsLibrary Example[867:18e03] Assets Type : 'ALAssetTypePhoto' 2015-12-21 18:33:03.658 ALAssetsLibrary Example[867:18e03] Assets Location : '(null)' 2015-12-21 18:33:03.659 ALAssetsLibrary Example[867:18e03] Assets Duration : 'ALErrorInvalidProperty' 2015-12-21 18:33:03.660 ALAssetsLibrary Example[867:18e03] Assets Orientation : '0' 2015-12-21 18:33:03.663 ALAssetsLibrary Example[867:18e03] Assets created Date : '2010-04-11 15:01:49 +0000' 2015-12-21 18:33:03.666 ALAssetsLibrary Example[867:18e03] Assets Representations : 'public.jpeg' 2015-12-21 18:33:03.667 ALAssetsLibrary Example[867:18e03] Assets URLs : 'assets-library://asset/asset.JPG?id=8BA2376C-778E-490B-BB76-F71ABCB861AD&ext=JPG' 2015-12-21 18:33:03.667 ALAssetsLibrary Example[867:18e03] Stop? : YES 2015-12-21 18:33:03.668 ALAssetsLibrary Example[867:18e03] Assets Type : 'ALAssetTypePhoto' 2015-12-21 18:33:03.669 ALAssetsLibrary Example[867:18e03] Assets Location : '(null)' 2015-12-21 18:33:03.670 ALAssetsLibrary Example[867:18e03] Assets Duration : 'ALErrorInvalidProperty' 2015-12-21 18:33:03.671 ALAssetsLibrary Example[867:18e03] Assets Orientation : '0' 2015-12-21 18:33:03.673 ALAssetsLibrary Example[867:18e03] Assets created Date : '2013-10-25 10:07:54 +0000' 2015-12-21 18:33:03.674 ALAssetsLibrary Example[867:18e03] Assets Representations : 'public.jpeg' 2015-12-21 18:33:03.676 ALAssetsLibrary Example[867:18e03] Assets URLs : 'assets-library://asset/asset.JPG?id=A7E07162-3773-445B-90DA-C468AAF265C9&ext=JPG' 2015-12-21 18:33:03.677 ALAssetsLibrary Example[867:18e03] Stop? : YES 2015-12-21 18:33:03.678 ALAssetsLibrary Example[867:18e03] Assets Type : 'ALAssetTypePhoto' 2015-12-21 18:33:03.679 ALAssetsLibrary Example[867:18e03] Assets Location : '(null)' 2015-12-21 18:33:03.680 ALAssetsLibrary Example[867:18e03] Assets Duration : 'ALErrorInvalidProperty' 2015-12-21 18:33:03.681 ALAssetsLibrary Example[867:18e03] Assets Orientation : '0' 2015-12-21 18:33:03.682 ALAssetsLibrary Example[867:18e03] Assets created Date : '2015-12-09 08:07:21 +0000' 2015-12-21 18:33:03.683 ALAssetsLibrary Example[867:18e03] Assets Representations : 'public.jpeg' 2015-12-21 18:33:03.684 ALAssetsLibrary Example[867:18e03] Assets URLs : 'assets-library://asset/asset.JPG?id=C85F1F40-5144-4944-B3E9-8265C9F255B5&ext=JPG' 2015-12-21 18:33:03.685 ALAssetsLibrary Example[867:18e03] Stop? : YES 2015-12-21 18:33:03.686 ALAssetsLibrary Example[867:18e03] Assets Type : 'ALAssetTypePhoto' 2015-12-21 18:33:03.687 ALAssetsLibrary Example[867:18e03] Assets Location : '(null)' 2015-12-21 18:33:03.689 ALAssetsLibrary Example[867:18e03] Assets Duration : 'ALErrorInvalidProperty' 2015-12-21 18:33:03.690 ALAssetsLibrary Example[867:18e03] Assets Orientation : '0' 2015-12-21 18:33:03.691 ALAssetsLibrary Example[867:18e03] Assets created Date : '2015-12-09 08:07:54 +0000' 2015-12-21 18:33:03.692 ALAssetsLibrary Example[867:18e03] Assets Representations : 'public.jpeg' 2015-12-21 18:33:03.693 ALAssetsLibrary Example[867:18e03] Assets URLs : 'assets-library://asset/asset.JPG?id=4252A972-654B-4E17-B179-1AF2F57B749D&ext=JPG' 2015-12-21 18:33:03.694 ALAssetsLibrary Example[867:18e03] Stop? : YES 2015-12-21 18:33:03.695 ALAssetsLibrary Example[867:18e03] Assets Type : '(null)' 2015-12-21 18:33:03.696 ALAssetsLibrary Example[867:18e03] Assets Location : '(null)' 2015-12-21 18:33:03.697 ALAssetsLibrary Example[867:18e03] Assets Duration : '(null)' 2015-12-21 18:33:03.698 ALAssetsLibrary Example[867:18e03] Assets Orientation : '(null)' 2015-12-21 18:33:03.699 ALAssetsLibrary Example[867:18e03] Assets created Date : '(null)' 2015-12-21 18:33:03.700 ALAssetsLibrary Example[867:18e03] Assets Representations : '(null)' 2015-12-21 18:33:03.701 ALAssetsLibrary Example[867:18e03] Assets URLs : '(null)' 2015-12-21 18:33:03.703 ALAssetsLibrary Example[867:18e03] ........................ 2015-12-21 18:33:03.705 ALAssetsLibrary Example[867:18e03] Group Name : '(null)' 2015-12-21 18:33:03.706 ALAssetsLibrary Example[867:18e03] Group Type : '(null)' 2015-12-21 18:33:03.706 ALAssetsLibrary Example[867:18e03] Group ID : '(null)' 2015-12-21 18:33:03.707 ALAssetsLibrary Example[867:18e03] Group URL : '(null)

Updated 07-Sep-2019

Leave Comment

Comments

Liked By